﻿/**
 * Created by WU on 2016/9/8.
 */
var express = require('express');
var router = express.Router();
var commentDao=require('../dao/commentDao');

var util=require('util');
var filter=require('../util/filter');


/* GET users listing.*/
router.get('/', function(req, res) {
    req.session.urldate='article';
    var index=1;
    var account=5;
    commentDao.getBooks(index,account,function(result){
        console.log('route.books'+JSON.stringify(result));
            res.render('talk_about_pet',{books:result,user:req.session.userID,username:req.session.userID});

    })
});
router.get('/:id', function(req, res){
    console.log('user ' + req.params.id);
    var index=parseInt(req.params.id);
    console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    console.log("下标为"+index);
    commentDao.get_details_books(index,function(result){
        if(req.session.userID==undefined){
            res.redirect('/login');
        }else {
            commentDao.comment(index,function(result_com){
                if(req.session.userID==undefined){
                    res.redirect('/login');
                }else {
                    res.render('index',{books:result,user:req.session.userID,comment:result_com,artno:index,username:req.session.userID});
                }
            })
        }
    });
});
router.post('/', function(req, res){

    var date=new Date();
    if(req.body.artno){
        commentDao.add_comment(req.session.usertel,req.body.beforeno,req.body.artno,req.body.comment,date,function(result){
        res.json({res:'0'});
    });}
    else if(req.body.like=='1'){
        commentDao.check_like_article(req.body.like_artno,function(result){
            if(result.length!=0){
                res.json({likes:'10'});
            }else{
                commentDao.like_article(req.session.usertel,req.body.like_artno,function(result){
                    res.json({likes:'1'});
                });
            }
        });
    }else if(req.body.page){
        var index=parseInt(req.body.page)+2;
        var account=1;
        commentDao.getBooks(index,account,function(result){
                res.json({res:result});
        })

    }

});
module.exports = router;